const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");
const Supplier = require("./Supplier");
const Category = require("./Category");

const Product = sequelize.define("Product", {
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '商品名称'
  },
  purchasePrice: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '进货价'
  },
  salePrice: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    defaultValue: 0,
    comment: '销售价'
  },
  stock: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '库存数量'
  },
  warningStock: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '预警库存'
  },
  supplierId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '公司ID'
  },
  categoryId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '分类ID'
  }
}, {
  tableName: 'products',
  timestamps: true,
  comment: '商品表'
});

// 建立与公司的关联关系
Product.belongsTo(Supplier, {
  foreignKey: 'supplierId',
  as: 'supplier'
});

// 建立与分类的关联关系
Product.belongsTo(Category, {
  foreignKey: 'categoryId',
  as: 'category'
});

module.exports = Product;
